package de.tum.in.eist.im.client.Chat;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.LongBox;

import de.tum.in.eist.im.client.Chat.ChatChannel.ChatMessageHandler;
import de.tum.in.eist.im.shared.ChatChannel.ChatChannel;
import de.tum.in.eist.im.shared.User.User;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class ChatUI extends Composite {

	private static ChatUIUiBinder uiBinder = GWT.create(ChatUIUiBinder.class);
	@UiField TabLayoutPanel tabPanel;
	@UiField HorizontalPanel messageInputPanel;
	@UiField Button sendButton;
	@UiField LongBox messageBox;
	@UiField FlexTable chatTable;
	private ArrayList<ChatBox> chatBoxList = new ArrayList<ChatBox>();
	private List<Long> openTabs = new ArrayList<Long>();
	private User user;
	private ChatUIHandler chatUIHandler;

	interface ChatUIUiBinder extends UiBinder<Widget, ChatUI> {
	}

	public ChatUI(User user) {
		initWidget(uiBinder.createAndBindUi(this));
		this.user = user;
		chatUIHandler = new ChatUIHandler(this, this.user);
		newChat("General");
	}
	
	public void requestChatTab(String name)
	{
		chatUIHandler.getChannelId(name);
	}
	
	public ChatBox getCurrentTab()
	{
		int index = tabPanel.getSelectedIndex();
		ChatBox currentTab = chatBoxList.get(index);
		
		return currentTab;
	}
	
	private void newChat(String name)
	{
		ChatBox newBox = new ChatBox();
		chatBoxList.add(newBox);
		tabPanel.add(newBox,name);
		newBox.addMessage("You joined the "+name+" Channel");
	}

	public void addChatTab(String channelTitle, Long chatChannelId) {
		
		// check if you got a valid channel id
		if (-1 == chatChannelId) {
			return;
		}
		
		boolean foundTab = false;
		for (Long o : openTabs) {
			if (o.equals(chatChannelId))
				foundTab = true;
		}
		if (foundTab)
			return;
		
		// create chat message handler and send message panel
		//to be removed.
		ChatMessageHandler chatMessageHandler = new ChatMessageHandler(chatChannelId, this.user);
		
		openTabs.add(chatChannelId);
		newChat(channelTitle);
		
	}

	public void clearListOfChatChannels() {
		// TODO Auto-generated method stub
		
	}

	public void addChatChannelToListOfOpenChannels(ChatChannel cC) {
		// TODO Auto-generated method stub
		
	}

	public void clearListOfOnlineUser() {
		// TODO Auto-generated method stub
		
	}

	public void addUserToListOfOnlineUser(User user2) {
		// TODO Auto-generated method stub
	}

	@UiHandler("sendButton")
	void onSendButtonClick(ClickEvent event) {
		(getCurrentTab()).addMessage(messageBox.getText());
		messageBox.setText("");
	}
}
